package com.leyou;

import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;

/**
 * 系统响应数据必备字段
 */
@Getter
@Setter
@ToString
@EqualsAndHashCode
public class ResponseData<T> {

    private int status;//状态码
    private String msg;
    private Long timestamp;
    private String version;
    private String platForm; // 0:未知; 1:Android App; 2:IOS App; 3:Web; 4:WeChat; 5:支付宝小程序;
    private T data;

    public ResponseData() {
        this.status = 200;
        this.timestamp = System.currentTimeMillis();
    }

    public ResponseData(T data) {
        this();
        this.data = data;
    }

    public ResponseData(Parameter parameter, T data) {
        this(data);
        setParameter(parameter);
    }

    public ResponseData(int status, String msg) {
        this.status = status;
        this.msg = msg;
        this.timestamp = System.currentTimeMillis();
    }

    public ResponseData(int status, String msg, T data) {
        this(status, msg);
        this.data = data;
    }

    public ResponseData(int status, String msg, String version, String platForm, T data) {
        this(status, msg, data);
        this.version = version;
        this.platForm = platForm;
    }

    public ResponseData(int status, String msg, Parameter parameter, T data) {
        this(status, msg, data);
        setParameter(parameter);
    }

    public void setParameter(Parameter parameter) {
        if (parameter != null) {
            this.version = parameter.getVersion();
            this.platForm = parameter.getPlatForm();
        }
    }

    public static ResponseData ok() {
        return new ResponseData();
    }

    public static <T> ResponseData ok(T data) {
        ResponseData<Object> dataTmp = new ResponseData<>();
        dataTmp.setData(data);
        return dataTmp;
    }

    public static ResponseData error(int status,String msg,Parameter parameter) {
        ResponseData<Object> dataTmp = new ResponseData<>(status,msg);
        dataTmp.setParameter(parameter);
        return dataTmp;
    }

    public static ResponseData error() {
        ResponseData<Object> dataTmp = new ResponseData<>(500,"服务器繁忙");
        dataTmp.setParameter(null);
        return dataTmp;
    }
}
